package com.voute.View.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.baoyz.actionsheet.ActionSheet;
import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.SocializeUtils;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.FeedCommentBean;
import com.voute.Model.Bean.FeedCommentDataEntity;
import com.voute.Model.Bean.FeedDetailInfoBean;
import com.voute.Model.Bean.VoteResultBean;
import com.voute.Presenter.IFeedDetailInfoPresenter;
import com.voute.Presenter.Impl.FeedDetailInfoPresenterImpl;
import com.voute.R;
import com.voute.Utils.DisplayUtil;
import com.voute.Utils.GlideUtils;
import com.voute.Utils.PreferenceUtils;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.IView.IFeedDetailInfoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by liucxu on 2017/4/28.
 */
//@MLinkRouter(keys = "fivesec_mw_feeds")
public class FeedDetailInfoActivity extends BaseActivity implements IFeedDetailInfoView {

    @BindView(R.id.back_layout)
    RelativeLayout back_layout;
    @BindView(R.id.feed_star_imv)
    ImageView feed_star_imv;
    @BindView(R.id.star_layout)
    RelativeLayout star_layout;
    @BindView(R.id.share_layout)
    RelativeLayout share_layout;
    @BindView(R.id.report_layout)
    RelativeLayout report_layout;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.list_content_view)
    ListView list_content_view;
    @BindView(R.id.vote_or_comment_tv)
    TextView vote_or_comment_tv;

    private TextView tv_feed_title;
    private TextView tv_last_time;
    private TextView tv_circle_belonged;
    private TextView tv_join_circle;
    private ImageView feed_detail_imv;
    private TextView feed_desc;
    private TextView tv_vote_num;
    private TextView tv_left_vote_num;
    private TextView tv_left_title;
    private TextView tv_right_vote_num;
    private TextView tv_right_title;


    private TextView yes_count;
    private TextView no_count;
    private TextView click_me_yes;
    private TextView click_me_no;
    private ImageView count_imv;
    private LinearLayout close_dialog_layout;
    private View line_horzintal_line1;
    private View view_divider_horizontal;
    private RelativeLayout layout_feed_all_vote;


    private String hash_feed;
    private FeedDetailInfoBean feed_detail;
    private IFeedDetailInfoPresenter presenter;

    private MaterialDialog progressDialog;
    private Dialog vote_dialog;
    private View header_view;
    private View vote_view;
    private CommentAdapter adapter;
    private List<FeedCommentDataEntity> datas = new ArrayList<>();


    private int click_num = 0;
    private int click_num_to_upload = 0;
    private boolean clicked_positive = true;
    private boolean is_first_click = true;
    private CountDownTimer timer;
    String fid = "";

    private Dialog share_voted_dialog;
    private View share_view_voted;
    private TextView tv_positive_vote_num;
    private TextView tv_negative_vote_num;
    private LinearLayout wechat_circle_layout;
    private LinearLayout we_chat_layout;
    private LinearLayout weibo_layout;
    private RelativeLayout top_layout;
    private LinearLayout QQ_layout_share_after_vote;
    private LinearLayout close_dialog_layout_share;
    private TextView tv_left_title_option;
    private TextView tv_right_title_option;
    private TextView tv_vote_num_mine;
    private TextView tv_share_title_voted;


    private Dialog share_unvote_dialog;
    private View share_view_unvote;
    private LinearLayout wechat_circle_layout_share;
    private LinearLayout we_chat_layout_share;
    private LinearLayout QQ_layout_share;
    private LinearLayout weibo_layout_share;
    private LinearLayout close_dialog_layout_share_share;
    private TextView tv_share_title_unvoted;

    private View.OnClickListener share_listener;
    private UMImage imageurl;

    //分页相关参数
    private boolean isRefresh = true;
    private boolean stopLoadMore;
    private boolean isLoading;
    private int page = 1;

    //举报
    private MaterialDialog report_dialog;
    private View view_report_dialog;
    private EditText edt_report_reson;
    private TextView tv_close_dialog;
    private TextView tv_submit_report;

    private TextView yes_tv;
    private TextView no_tv;

    //画面来源
    private boolean from_create = false;

    private Animation sacle_animation;

    private boolean isMlink=false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feed_info_details;
    }


//    @Override
//    public void onBackPressed() {
//        if (App.GetInstance().getActivities().size() == 1) {
//            Intent intent = new Intent(FeedDetailInfoActivity.this, MainActivity.class);
//            startActivity(intent);
//            App.GetInstance().finishActivity(FeedDetailInfoActivity.this);
//        } else {
//            App.GetInstance().finishActivity(FeedDetailInfoActivity.this);
//        }
//    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        hash_feed = getIntent().getStringExtra("KEY_FEED_HASH");
        from_create = getIntent().getBooleanExtra("KEY_FROM_CREATE", false);
        isMlink=getIntent().getBooleanExtra("mlink",false);
        String key_feed_mw="";
        //通过intent方式获取动态参数值
        Intent intent_mw = getIntent();
        if (intent_mw != null) {
            key_feed_mw = intent_mw.getStringExtra("fid");
            Logger.i(key_feed_mw);
        }
        if(!TextUtils.isEmpty(key_feed_mw)){
            hash_feed=key_feed_mw;
        }
        if (TextUtils.isEmpty(hash_feed)) {
            App.GetInstance().finishActivity(FeedDetailInfoActivity.this);
        }
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isMlink){
                    Intent intent = new Intent(FeedDetailInfoActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                App.GetInstance().finishActivity(FeedDetailInfoActivity.this);
            }
        });
        adapter = new CommentAdapter(datas);
        list_content_view.setAdapter(adapter);
        sacle_animation = AnimationUtils.loadAnimation(this, R.anim.scale_x_y_animation);
        /**
         * 自定义投票Dialog
         */
        vote_view = View.inflate(this, R.layout.dialog_vote_layout, null);
        yes_tv = (TextView) vote_view.findViewById(R.id.yes_tv);
        no_tv = (TextView) vote_view.findViewById(R.id.no_tv);
        yes_count = (TextView) vote_view.findViewById(R.id.yes_count);
        no_count = (TextView) vote_view.findViewById(R.id.no_count);
        click_me_yes = (TextView) vote_view.findViewById(R.id.click_me_yes);
        click_me_no = (TextView) vote_view.findViewById(R.id.click_me_no);
        count_imv = (ImageView) vote_view.findViewById(R.id.count_imv);
        close_dialog_layout = (LinearLayout) vote_view.findViewById(R.id.close_dialog_layout);
        setVoteListeners();
        initVoteDialog();


        share_view_voted = View.inflate(this, R.layout.dialog_share_layout, null);
        share_view_unvote = View.inflate(this, R.layout.dialog_share_layout_share, null);        //分享按钮
        initVotedShareDialogViews();
        initUnVoteShareDialogViews();
        setShareListeners();
        share_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (feed_detail.getData().isVoted()) {
                    if (share_voted_dialog == null) {
                        initVotedShareDialog();
                    }
                    if ("left".equals(feed_detail.getData().getSide())) {
                        tv_positive_vote_num.setTextColor(Color.parseColor("#fe3768"));
                        tv_negative_vote_num.setTextColor(Color.parseColor("#333333"));
                        tv_vote_num_mine.setText("为" + feed_detail.getData().getLeft_option() + "投了" + feed_detail.getData().getSelf_vote() + "票");
                    } else {
                        tv_positive_vote_num.setTextColor(Color.parseColor("#333333"));
                        tv_negative_vote_num.setTextColor(Color.parseColor("#fe3768"));
                        tv_vote_num_mine.setText("为" + feed_detail.getData().getRight_option() + "投了" + feed_detail.getData().getSelf_vote() + "票");
                    }
                    if (feed_detail.getData().getCircle() != null) {
                        tv_share_title_voted.setText("分享，邀请好友加入这个圈子");
                    } else {
                        tv_share_title_voted.setText("分享到");
                    }
                    tv_positive_vote_num.setText(feed_detail.getData().getLeft_vote() + "");
                    tv_negative_vote_num.setText(feed_detail.getData().getRight_vote() + "");
                    tv_left_title_option.setText(feed_detail.getData().getLeft_option());
                    tv_right_title_option.setText(feed_detail.getData().getRight_option());
                    share_voted_dialog.show();
                } else {
                    if (share_unvote_dialog == null) {
                        initUnVoteShareDialog();
                    }
                    if (feed_detail.getData().getCircle() != null) {
                        tv_share_title_unvoted.setText("分享，邀请好友加入这个圈子");
                    } else {
                        tv_share_title_unvoted.setText("分享到");
                    }
                    share_unvote_dialog.show();
                }
            }
        });

        report_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkLoginStatus()) {
                    ActionSheet.createBuilder(FeedDetailInfoActivity.this, getSupportFragmentManager())
                            .setCancelButtonTitle("取消")
                            .setOtherButtonTitles("举报")
                            .setCancelableOnTouchOutside(true)
                            .setListener(new ActionSheet.ActionSheetListener() {
                                @Override
                                public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

                                }

                                @Override
                                public void onOtherButtonClick(ActionSheet actionSheet, int index) {
                                    if (index == 0) {
                                        if (report_dialog == null) {
                                            view_report_dialog = View.inflate(FeedDetailInfoActivity.this, R.layout.dialog_report, null);
                                            edt_report_reson = (EditText) view_report_dialog.findViewById(R.id.edt_report_reson);
                                            tv_close_dialog = (TextView) view_report_dialog.findViewById(R.id.tv_close_dialog);
                                            tv_close_dialog.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    report_dialog.dismiss();
                                                }
                                            });
                                            tv_submit_report = (TextView) view_report_dialog.findViewById(R.id.tv_submit_report);
                                            tv_submit_report.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    if (TextUtils.isEmpty(edt_report_reson.getText().toString())) {
                                                        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确认").cancelable(false).content("请输入举报原因(6~255字)").build();
                                                        notice_dialog.show();
                                                    } else {
                                                        if (edt_report_reson.getText().toString().length() < 6) {
                                                            MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确认").cancelable(false).content("请输入举报原因(6~255字)").build();
                                                            notice_dialog.show();
                                                        } else {
                                                            presenter.reportFeed(hash_feed, edt_report_reson.getText().toString());
                                                            edt_report_reson.setText("");
                                                        }
                                                    }
                                                }
                                            });
                                            report_dialog = new MaterialDialog.Builder(FeedDetailInfoActivity.this).customView(view_report_dialog, false).cancelable(false).build();
                                        }
                                        report_dialog.show();
                                    }
                                }
                            }).show();
                }

            }
        });
//        评论分页
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                getPagedComments();
            }
        });

        list_content_view.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int last_visiable = absListView.getLastVisiblePosition();
                if (!stopLoadMore && !isLoading && (last_visiable > 0) && (!TextUtils.isEmpty(PreferenceUtils.GetUserToken()))) {
                    if (last_visiable + 2 > totalItemCount) {
                        isRefresh = false;
                        getPagedComments();
                    }
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(isMlink){
            Intent intent = new Intent(FeedDetailInfoActivity.this, MainActivity.class);
            startActivity(intent);
        }
        App.GetInstance().finishActivity(FeedDetailInfoActivity.this);
    }

    private void getPagedComments() {
        int page_local = 1;
        if (isRefresh)
            page_local = 1;
        else {
            if (datas.size() == 0) {
                page_local = 1;
            } else {
                page_local = page;
            }
        }
        if (!isRefresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                isLoading = true;
            }
        } else {
            isLoading = true;
            stopLoadMore = true;
        }
        presenter.requestComments(hash_feed, page_local + "");
    }

    private UMShareListener shareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA share_media) {
            SocializeUtils.safeShowDialog(progressDialog);
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(FeedDetailInfoActivity.this, "成功了", Toast.LENGTH_LONG).show();
            SocializeUtils.safeCloseDialog(progressDialog);
            if (share_voted_dialog != null) {
                if (share_voted_dialog.isShowing()) {
                    share_voted_dialog.dismiss();
                }
            }
            if (share_unvote_dialog != null) {
                if (share_unvote_dialog.isShowing()) {
                    share_unvote_dialog.dismiss();
                }
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            SocializeUtils.safeCloseDialog(progressDialog);
            Toast.makeText(FeedDetailInfoActivity.this, "分享失败" + throwable.getMessage(), Toast.LENGTH_LONG).show();
            if (share_voted_dialog != null) {
                if (share_voted_dialog.isShowing()) {
                    share_voted_dialog.dismiss();
                }
            }
            if (share_unvote_dialog != null) {
                if (share_unvote_dialog.isShowing()) {
                    share_unvote_dialog.dismiss();
                }
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            SocializeUtils.safeCloseDialog(progressDialog);
            Toast.makeText(FeedDetailInfoActivity.this, "分享取消了", Toast.LENGTH_LONG).show();
            if (share_voted_dialog != null) {
                if (share_voted_dialog.isShowing()) {
                    share_voted_dialog.dismiss();
                }
            }
            if (share_unvote_dialog != null) {
                if (share_unvote_dialog.isShowing()) {
                    share_unvote_dialog.dismiss();
                }
            }
        }
    };

    private void setShareListeners() {
        share_listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = view.getId();
                String share_url = feed_detail.getData().getShare().getLink();
                imageurl = new UMImage(FeedDetailInfoActivity.this, feed_detail.getData().getShare().getPic());
                imageurl.setThumb(new UMImage(FeedDetailInfoActivity.this, feed_detail.getData().getShare().getPic()));
                imageurl.setTitle(feed_detail.getData().getShare().getTitle());
                imageurl.setDescription(feed_detail.getData().getShare().getDesc());


                UMWeb uweb_local = new UMWeb(share_url);
                uweb_local.setTitle(feed_detail.getData().getShare().getTitle());
                uweb_local.setThumb(imageurl);
                uweb_local.setDescription(feed_detail.getData().getShare().getDesc());


                switch (id) {
                    case R.id.wechat_circle_layout:
                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.we_chat_layout:
                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.weibo_layout:
                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.wechat_circle_layout_share:

                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.we_chat_layout_share:
                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.weibo_layout_share:
                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                    case R.id.QQ_layout_share:
//                        new ShareAction(FeedDetailInfoActivity.this)
//                                .withText(feed_detail.getData().getShare().getDesc())
////                                .withMedia(imageurl)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
//                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();


                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();


                        break;
                    case R.id.QQ_layout_share_after_vote:
//                        new ShareAction(FeedDetailInfoActivity.this)
//                                .withText(feed_detail.getData().getShare().getDesc())
////                                .withMedia(imageurl)
//                                .withTargetUrl(feed_detail.getData().getShare().getLink())
//                                .withTitle(feed_detail.getData().getShare().getTitle())
//                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();

                        new ShareAction(FeedDetailInfoActivity.this)
                                .withText(feed_detail.getData().getShare().getTitle())
                                .withMedia(uweb_local)
                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
                                .setCallback(shareListener).share();
                        break;
                }
            }
        };
        QQ_layout_share_after_vote.setOnClickListener(share_listener);
        wechat_circle_layout.setOnClickListener(share_listener);
        we_chat_layout.setOnClickListener(share_listener);
        weibo_layout.setOnClickListener(share_listener);

        wechat_circle_layout_share.setOnClickListener(share_listener);
        we_chat_layout_share.setOnClickListener(share_listener);
        weibo_layout_share.setOnClickListener(share_listener);
        QQ_layout_share.setOnClickListener(share_listener);
    }

    private void initVotedShareDialogViews() {
        tv_left_title_option = (TextView) share_view_voted.findViewById(R.id.tv_left_title);
        tv_right_title_option = (TextView) share_view_voted.findViewById(R.id.tv_right_title);
        tv_positive_vote_num = (TextView) share_view_voted.findViewById(R.id.tv_positive_vote_num);
        tv_vote_num_mine = (TextView) share_view_voted.findViewById(R.id.tv_vote_num_mine);
        tv_share_title_voted = (TextView) share_view_voted.findViewById(R.id.tv_share_title_voted);
        tv_negative_vote_num = (TextView) share_view_voted.findViewById(R.id.tv_negative_vote_num);
        wechat_circle_layout = (LinearLayout) share_view_voted.findViewById(R.id.wechat_circle_layout);
        we_chat_layout = (LinearLayout) share_view_voted.findViewById(R.id.we_chat_layout);
        weibo_layout = (LinearLayout) share_view_voted.findViewById(R.id.weibo_layout);
        top_layout = (RelativeLayout) share_view_voted.findViewById(R.id.top_layout);
        QQ_layout_share_after_vote = (LinearLayout) share_view_voted.findViewById(R.id.QQ_layout_share_after_vote);
        close_dialog_layout_share = (LinearLayout) share_view_voted.findViewById(R.id.close_dialog_layout_share);
        close_dialog_layout_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                share_voted_dialog.dismiss();
            }
        });

    }

    private void initUnVoteShareDialogViews() {
        wechat_circle_layout_share = (LinearLayout) share_view_unvote.findViewById(R.id.wechat_circle_layout_share);
        we_chat_layout_share = (LinearLayout) share_view_unvote.findViewById(R.id.we_chat_layout_share);
        tv_share_title_unvoted = (TextView) share_view_unvote.findViewById(R.id.tv_share_title_unvoted);
        QQ_layout_share = (LinearLayout) share_view_unvote.findViewById(R.id.QQ_layout_share);
        weibo_layout_share = (LinearLayout) share_view_unvote.findViewById(R.id.weibo_layout_share);
        close_dialog_layout_share_share = (LinearLayout) share_view_unvote.findViewById(R.id.close_dialog_layout_share_share);
        close_dialog_layout_share_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share_unvote_dialog.dismiss();
            }
        });
    }

    private void setVoteListeners() {
        close_dialog_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (click_num != 0) {
                    initTimerCount();
                    if (clicked_positive) {
                        presenter.voteFeed(hash_feed, click_num + "", "1");
                    } else {
                        presenter.voteFeed(hash_feed, click_num + "", "0");
                    }
                }
                vote_dialog.dismiss();
            }
        });
        click_me_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click_me_no.setEnabled(false);
                click_me_yes.setEnabled(true);
                clicked_positive = true;
                click_num = click_num + 1;
                yes_count.setTextColor(Color.parseColor("#fd3768"));
                yes_count.setBackgroundResource(R.drawable.bg_vote_yes_count_selected);
                if (click_num < 10) {
                    yes_count.setText("00" + click_num);
                } else if ((click_num > 9) && (click_num < 100)) {
                    yes_count.setText("0" + click_num);
                } else {
                    yes_count.setText("" + click_num);
                }
                if (is_first_click) {
                    initTimerCount();
                    timer.start();
                    is_first_click = false;
                }
            }
        });
        click_me_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click_me_no.setEnabled(true);
                click_me_yes.setEnabled(false);
                clicked_positive = false;
                click_num = click_num + 1;
                no_count.setTextColor(Color.parseColor("#fd3768"));
                no_count.setBackgroundResource(R.drawable.bg_vote_yes_count_selected);
                if (click_num < 10) {
                    no_count.setText("00" + click_num);
                } else if ((click_num > 9) && (click_num < 100)) {
                    no_count.setText("0" + click_num);
                } else {
                    no_count.setText("" + click_num);
                }
                if (is_first_click) {
                    initTimerCount();
                    timer.start();
                    is_first_click = false;
                }
            }
        });
    }


    private void initTimerCount() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long l) {
                int remain = (int) l / 1000;
                switch (remain) {
                    case 5:
                        count_imv.setImageResource(R.mipmap.icon_djs5);
                        break;
                    case 4:
                        count_imv.setImageResource(R.mipmap.icon_djs4);
                        break;
                    case 3:
                        count_imv.setImageResource(R.mipmap.icon_djs3);
                        break;
                    case 2:
                        count_imv.setImageResource(R.mipmap.icon_djs2);
                        break;
                    case 1:
                        count_imv.setImageResource(R.mipmap.icon_djs1);
                        break;
                    case 0:
                        break;
                }
            }

            @Override
            public void onFinish() {
                count_imv.setImageResource(R.mipmap.icon_djs0);
                vote_dialog.dismiss();
                if (clicked_positive) {
                    presenter.voteFeed(hash_feed, click_num + "", "1");
                } else {
                    presenter.voteFeed(hash_feed, click_num + "", "0");
                }
                click_num_to_upload = click_num;
                click_num = 0;
            }
        };
    }

    private void initVoteDialog() {
        vote_dialog = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = vote_dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        vote_dialog.setCancelable(false);
        vote_dialog.setContentView(vote_view);
    }

    @Override
    public void setupData() {
        presenter = new FeedDetailInfoPresenterImpl(this);
        header_view = View.inflate(this, R.layout.header_feed_details_info, null);

        tv_feed_title = (TextView) header_view.findViewById(R.id.tv_feed_title);
        tv_last_time = (TextView) header_view.findViewById(R.id.tv_last_time);
        tv_circle_belonged = (TextView) header_view.findViewById(R.id.tv_circle_belonged);
        tv_join_circle = (TextView) header_view.findViewById(R.id.tv_join_circle);
        feed_detail_imv = (ImageView) header_view.findViewById(R.id.feed_detail_imv);
        feed_desc = (TextView) header_view.findViewById(R.id.feed_desc);
        tv_vote_num = (TextView) header_view.findViewById(R.id.tv_vote_num);
        tv_left_vote_num = (TextView) header_view.findViewById(R.id.tv_left_vote_num);
        tv_left_title = (TextView) header_view.findViewById(R.id.tv_left_title);
        tv_right_vote_num = (TextView) header_view.findViewById(R.id.tv_right_vote_num);
        tv_right_title = (TextView) header_view.findViewById(R.id.tv_right_title);
        line_horzintal_line1 = (View) header_view.findViewById(R.id.line_horzintal_line1);
        view_divider_horizontal = (View) header_view.findViewById(R.id.view_divider_horizontal);
        layout_feed_all_vote = (RelativeLayout) header_view.findViewById(R.id.layout_feed_all_vote);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.queryFeedDetailInfo(hash_feed);
        if (!TextUtils.isEmpty(PreferenceUtils.GetUserToken())) {
            isRefresh = true;
            getPagedComments();
        }
        MobclickAgent.onEvent(this, "view_feed");
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(this)
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        swipe_refresh_layout.setRefreshing(false);
        progressDialog.dismiss();
    }

    @Override
    public void showError(String errMsg) {
        swipe_refresh_layout.setRefreshing(false);
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    @Override
    public void setFeedDetailInfo(FeedDetailInfoBean response) {
        yes_tv.setText(response.getData().getLeft_option());
        no_tv.setText(response.getData().getRight_option());
        swipe_refresh_layout.setRefreshing(false);
        feed_detail = response;
        tv_feed_title.setText(feed_detail.getData().getTitle());
        if (TextUtils.isEmpty(feed_detail.getData().getLive_time())) {
            tv_last_time.setVisibility(View.INVISIBLE);
        } else {
            tv_last_time.setVisibility(View.VISIBLE);
            tv_last_time.setText(feed_detail.getData().getLive_time());
        }
        if (feed_detail.getData().getCircle() == null) {
            tv_circle_belonged.setVisibility(View.GONE);
            tv_join_circle.setVisibility(View.GONE);
            tv_last_time.setVisibility(View.GONE);
        } else {
            if (feed_detail.getData().getCircle().isJoined()) {
                tv_join_circle.setBackgroundResource(R.drawable.bg_joined_circle);
                tv_join_circle.setTextColor(Color.parseColor("#999999"));
                tv_join_circle.setText("已加入");
                tv_join_circle.setClickable(false);
                tv_join_circle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确定").negativeText("取消").content("退出此圈子?").onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                presenter.exitCircle(feed_detail.getData().getCircle().getHash());
                            }
                        }).show();
                    }
                });
                tv_join_circle.clearAnimation();
            } else {
                tv_join_circle.setBackgroundResource(R.drawable.bg_join_circle);
                tv_join_circle.setTextColor(Color.parseColor("#fe3768"));
                tv_join_circle.setText("+  加入");
                tv_join_circle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (checkLoginStatus()) {
                            presenter.joinCircle(feed_detail.getData().getCircle().getHash());
                        }
                    }
                });
                tv_join_circle.clearAnimation();
                sacle_animation.setAnimationListener(new RepeatAnimationListener());
                tv_join_circle.startAnimation(sacle_animation);
                sacle_animation.startNow();
            }
            tv_join_circle.setVisibility(View.VISIBLE);
            tv_circle_belonged.setVisibility(View.VISIBLE);
            tv_circle_belonged.setText(feed_detail.getData().getCircle().getName());
            tv_circle_belonged.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(FeedDetailInfoActivity.this, CircleActivity.class);
                    intent.putExtra("KEY_CIRCLE_TITLE", feed_detail.getData().getCircle().getName());
                    intent.putExtra("KEY_CIRCLE_HASH", feed_detail.getData().getCircle().getHash());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    FeedDetailInfoActivity.this.startActivity(intent);
                }
            });
        }
        if (TextUtils.isEmpty(feed_detail.getData().getPic())) {
            feed_detail_imv.setVisibility(View.GONE);
        } else {
            feed_detail_imv.setVisibility(View.VISIBLE);
            feed_detail_imv.setMaxHeight(9 * (DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(App.GetInstance(), 24)) / 16);
            GlideUtils.getInstance().loadSimpleContextVitMap(this, feed_detail.getData().getPic(), feed_detail_imv);
        }
        if (TextUtils.isEmpty(feed_detail.getData().getDescription())) {
            feed_desc.setVisibility(View.GONE);
        } else {
            feed_desc.setVisibility(View.VISIBLE);
            feed_desc.setText(feed_detail.getData().getDescription());
        }
        if (feed_detail.getData().isVoted()) {
            layout_feed_all_vote.setVisibility(View.GONE);
            view_divider_horizontal.setVisibility(View.VISIBLE);
        } else {
            layout_feed_all_vote.setVisibility(View.VISIBLE);
            view_divider_horizontal.setVisibility(View.GONE);
            tv_vote_num.setText("已经有" + feed_detail.getData().getAll_vote() + "票");
        }
        tv_left_title.setText(feed_detail.getData().getLeft_option());
        tv_right_title.setText(feed_detail.getData().getRight_option());
        if (response.getData().isVoted()) {
            tv_left_vote_num.setText(feed_detail.getData().getLeft_vote() + "票");
            tv_right_vote_num.setText(feed_detail.getData().getRight_vote() + "票");
            if ("left".equals(feed_detail.getData().getSide())) {
                tv_left_vote_num.setTextColor(Color.parseColor("#ffffff"));
                tv_right_vote_num.setTextColor(Color.parseColor("#333333"));
                tv_left_vote_num.setBackgroundResource(R.drawable.bg_pink_rigth_vote_num);
                tv_right_vote_num.setBackgroundResource(R.drawable.bg_grey_left_vote_num);
            } else {
                tv_left_vote_num.setTextColor(Color.parseColor("#333333"));
                tv_right_vote_num.setTextColor(Color.parseColor("#ffffff"));
                tv_left_vote_num.setBackgroundResource(R.drawable.bg_grey_left_vote_num);
                tv_right_vote_num.setBackgroundResource(R.drawable.bg_pink_rigth_vote_num);
            }
            tv_left_vote_num.setVisibility(View.VISIBLE);
            tv_right_vote_num.setVisibility(View.VISIBLE);
            vote_or_comment_tv.setText("写评论");
            vote_or_comment_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkLoginStatus()) {
                        Intent intent = new Intent(FeedDetailInfoActivity.this, PostCommentsActivity.class);
                        intent.putExtra("key_fid", hash_feed + "");
                        FeedDetailInfoActivity.this.startActivity(intent);
                    }
                }
            });
        } else {
            tv_left_vote_num.setVisibility(View.GONE);
            tv_right_vote_num.setVisibility(View.GONE);
            vote_or_comment_tv.setText("立即投票解锁更多精彩内容");
            vote_or_comment_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (checkLoginStatus()) {
                        vote();
                    }
                }
            });
            adapter.clearDatas();
        }
        if (list_content_view.getHeaderViewsCount() == 0) {
            list_content_view.addHeaderView(header_view);
        }
        if (from_create) {
            share_layout.performClick();
            from_create = false;
        }
    }

    @Override
    public void voteSuccess(VoteResultBean vote_response) {

        Map<String, String> params = new HashMap<>();
        params.put("user_hash", PreferenceUtils.GetUserIdHash());
        params.put("feed_hash", hash_feed);
        MobclickAgent.onEventValue(this, "vote_stat", params, click_num_to_upload);

        if (share_voted_dialog == null) {
            initVotedShareDialog();
        }
        feed_detail.getData().setVoted(true);
        if (clicked_positive) {
            tv_positive_vote_num.setTextColor(Color.parseColor("#fe3768"));
            tv_negative_vote_num.setTextColor(Color.parseColor("#333333"));
            tv_vote_num_mine.setText("为" + feed_detail.getData().getLeft_option() + "投了" + click_num_to_upload + "票");
        } else {
            tv_positive_vote_num.setTextColor(Color.parseColor("#333333"));
            tv_negative_vote_num.setTextColor(Color.parseColor("#fe3768"));
            tv_vote_num_mine.setText("为" + feed_detail.getData().getRight_option() + "投了" + click_num_to_upload + "票");
        }
        if (feed_detail.getData().getCircle() != null) {
            tv_share_title_voted.setText("分享，邀请好友加入这个圈子");
        } else {
            tv_share_title_voted.setText("分享到");
        }
        tv_positive_vote_num.setText(vote_response.getData().getLeft_vote() + "");
        tv_negative_vote_num.setText(vote_response.getData().getRight_vote() + "");
        tv_left_title_option.setText(feed_detail.getData().getLeft_option());
        tv_right_title_option.setText(feed_detail.getData().getRight_option());
        share_voted_dialog.show();
        presenter.queryFeedDetailInfo(hash_feed);
    }

    @Override
    public void joinCircleSuccess() {
        tv_join_circle.setBackgroundResource(R.drawable.bg_joined_circle);
        tv_join_circle.setTextColor(Color.parseColor("#999999"));
        tv_join_circle.setText("已加入");
        tv_join_circle.setClickable(false);
        tv_join_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确定").negativeText("取消").content("退出此圈子?").onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        presenter.exitCircle(feed_detail.getData().getCircle().getHash());
                    }
                }).show();
            }
        });
        tv_join_circle.clearAnimation();
    }

    @Override
    public void exitCircleSuccess() {
        tv_join_circle.setBackgroundResource(R.drawable.bg_join_circle);
        tv_join_circle.setTextColor(Color.parseColor("#fe3768"));
        tv_join_circle.setText("+  加入");
        tv_join_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.joinCircle(feed_detail.getData().getCircle().getHash());
            }
        });

        sacle_animation.setAnimationListener(new RepeatAnimationListener());
        tv_join_circle.startAnimation(sacle_animation);
        sacle_animation.startNow();
    }

    @Override
    public void setMoreComments(FeedCommentBean response) {
        swipe_refresh_layout.setRefreshing(false);
        if (response != null) {
            if (response.getStatus() == 0) {
                isLoading = false;
                if (isRefresh) {
                    page = 1;
                    datas.clear();
                    isRefresh = false;
                }
                if (response.getData() != null) {
                    if (response.getData().getComments() != null && response.getData().getComments().size() > 0) {
                        page = page + 1;
                        datas.addAll(response.getData().getComments());
                        adapter.setDatas(datas);
                        stopLoadMore = false;
                    } else {
                        stopLoadMore = true;
                    }
                } else {
                    isLoading = false;
                    stopLoadMore = false;
                }
            } else {
                isLoading = false;
                stopLoadMore = false;
            }
        } else {
            isLoading = false;
            stopLoadMore = false;
        }
    }

    @Override
    public void reportFeedSuccess() {
        report_dialog.dismiss();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailInfoActivity.this).title("提示").positiveText("确认").cancelable(false).content("举报成功").build();
        notice_dialog.show();
    }

    private void initUnVoteShareDialog() {
        share_unvote_dialog = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = share_unvote_dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        share_unvote_dialog.setCancelable(false);
        share_unvote_dialog.setContentView(share_view_unvote);
    }

    private void initVotedShareDialog() {
        share_voted_dialog = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = share_voted_dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        share_voted_dialog.setCancelable(false);
        share_voted_dialog.setContentView(share_view_voted);
    }


    private void vote() {
        yes_count.setText("000");
        no_count.setText("000");
        click_me_no.setEnabled(true);
        click_me_yes.setEnabled(true);
        click_num = 0;
        clicked_positive = true;
        is_first_click = true;
        if (vote_dialog == null) {
            initVoteDialog();
        }
        vote_dialog.show();
    }

    class CommentAdapter extends BaseAdapter {

        private List<FeedCommentDataEntity> datas = new ArrayList<>();

        public void clearDatas() {
            datas.clear();
            notifyDataSetChanged();
        }

        public CommentAdapter(List<FeedCommentDataEntity> sources) {
            if (sources != null) {
                datas.clear();
                datas.addAll(sources);
            }
        }

        public void setDatas(List<FeedCommentDataEntity> sources) {
            if (sources != null) {
                datas.clear();
                datas.addAll(sources);
                notifyDataSetChanged();
            }
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convert_view, ViewGroup viewGroup) {
            if (convert_view == null) {
                convert_view = View.inflate(FeedDetailInfoActivity.this, R.layout.layout_comment_adapter, null);
            }
            initViews(convert_view, position);
            return convert_view;
        }

        private void initViews(final View convert_view, int position) {
            final FeedCommentDataEntity entity = datas.get(position);
            RelativeLayout layout_comment_header = ViewHolderUtils.get(convert_view, R.id.layout_comment_header);
            TextView tv_comment_num_adapter = ViewHolderUtils.get(convert_view, R.id.tv_comment_num_adapter);
            CircleImageView imv_user_header = ViewHolderUtils.get(convert_view, R.id.imv_user_header);
            TextView tv_user_name = ViewHolderUtils.get(convert_view, R.id.tv_user_name);
            TextView tv_comment_time = ViewHolderUtils.get(convert_view, R.id.tv_comment_time);
            final TextView tv_vote = ViewHolderUtils.get(convert_view, R.id.tv_vote);
            TextView tv_comment_content = ViewHolderUtils.get(convert_view, R.id.tv_comment_content);
            if (position == 0) {
                layout_comment_header.setVisibility(View.VISIBLE);
                tv_comment_num_adapter.setText("评论(" + feed_detail.getData().getComment() + ")");
            } else {
                layout_comment_header.setVisibility(View.GONE);
            }
            GlideUtils.getInstance().loadSimpleContextVitMap(FeedDetailInfoActivity.this, entity.getAvatar(), imv_user_header);
            tv_user_name.setText(entity.getUsername());
            tv_comment_time.setText(entity.getAdd_date());
            tv_vote.setText(entity.getUp() + "");
            if (entity.isUped()) {
                tv_vote.setTextColor(Color.parseColor("#fd3768"));
                Drawable drawable = getResources().getDrawable(R.mipmap.adapter_positive_selected);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
                tv_vote.setCompoundDrawables(null, null, drawable, null);//画在右边
            } else {
                tv_vote.setTextColor(Color.parseColor("#b5b5b5"));
                Drawable drawable = getResources().getDrawable(R.mipmap.adapter_feed_vote);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
                tv_vote.setCompoundDrawables(null, null, drawable, null);//画在右边
            }
            if (entity.isUped()) {
                tv_vote.setEnabled(false);
                convert_view.setEnabled(false);
            } else {
                tv_vote.setEnabled(true);
                convert_view.setEnabled(true);
            }
            convert_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.upVote((entity.getCid() + ""));
                    tv_vote.setText((entity.getUp() + 1) + "");
                    entity.setUped(true);
                    tv_vote.setEnabled(false);
                    convert_view.setEnabled(false);
                    tv_vote.setTextColor(Color.parseColor("#fd3768"));
                    Drawable drawable = getResources().getDrawable(R.mipmap.adapter_positive_selected);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
                    tv_vote.setCompoundDrawables(null, null, drawable, null);//画在右边
                }
            });
            tv_comment_content.setText(entity.getContent());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public class RepeatAnimationListener implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            animation.reset();
            animation.setAnimationListener(new RepeatAnimationListener());
            animation.start();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}
